#include "mainwindow.h"
#include "ui_mainwindow.h"


using namespace cv;
using namespace std;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mdiArea = ui->mdiArea;

    cmdIniziaPTZ = ui->cmdSetDetecor;
    cmdGoPTZ = ui->cmdGoPoint;
    cmdSpirale= ui->cmdSpirale;
    cmdStopSpirale = ui->cmdStopSpirale;
    cmdStampa = ui->cmdStampa;
    cmdGoHome = ui->cmdGoHome;
    cmdSaveFrame = ui->cmdSaveFrame;
    cmdGoABS = ui->cdmGoABS;
    X = ui->editX;
    Y = ui->EditY;
    Z = ui->EditZ;
    _registra = false;
    x=0;
    y=0;
    z=0;

    //Output = ui->OutputText;

    //string str = "Ciao"+x;

   // Output->append(QString("Prova %1").arg(x));
   // Output->append("Prova");


    camVideo = new CSourceManager("TEST",mdiArea);
    camVideo->Init(this);

    PTZ = new myPTZ(camVideo);
    //PTZ->moveToThread(&cThread);
    //connect(&cThread,SIGNAL(started()),PTZ,SLOT(sl_createHomographyPT()));
    //cThread.start();

    //SOLO SE VOGLIO REGISTRARE DA STACCARE NORMALEMENTE
    _registratore = new CVideoOutput_JPG("imagesPT/", "Detector", 50);


    _setDetector = false;

    //Connetti ogni frame che arriva al disegnatore della vista

    connect(cmdGoPTZ,SIGNAL(clicked()),this,SLOT(sl_movePoint()));

    connect(camVideo,SIGNAL(onNewFrame(CImageRGB*)),this,SLOT(sl_drawDetercorView(CImageRGB*)));

    connect(cmdIniziaPTZ,SIGNAL(clicked()),this,SLOT(sl_onDetecor()));

    connect(cmdSpirale,SIGNAL(clicked()),this,SLOT(sl_moveSpirale()));

    connect(cmdStopSpirale,SIGNAL(clicked()),PTZ,SLOT(sl_stopSpirale()));

    connect(cmdStampa,SIGNAL(clicked()),this,SLOT(sl_stamp()));

    connect(cmdGoHome,SIGNAL(clicked()),this,SLOT(sl_moveHome()));

    connect(cmdSaveFrame,SIGNAL(clicked()),this,SLOT(sl_SaveFrame()));

    connect(cmdGoABS,SIGNAL(clicked()),this,SLOT(sl_movePointABS()));


}

void MainWindow::capturePosition()
{
    x = X->text().toInt();
    y = Y->text().toInt();
    z = Z->text().toInt();
}

void MainWindow::captureMoved()
{
    Pan = X->text().toFloat();
    Tilt = Y->text().toFloat();
    Zoom = Z->text().toInt();
}

void MainWindow::sl_movePoint()
{
    captureMoved();
    PTZ->movePoint(Pan,Tilt,Zoom);
}

void MainWindow::sl_movePointABS()
{
    captureMoved();
    PTZ->movePointABS(Pan,Tilt,Zoom);
}

void MainWindow::sl_SaveFrame()
{
    _registra = true;
}

void MainWindow::sl_moveHome()
{
    PTZ->GoPreset(CStr("marco_crowd_3"));

}

void MainWindow::sl_moveSpirale()
{
    PTZ->sl_SpiralDetection(5,1,30);
}


void MainWindow::sl_onDetecor()
{
    _setDetector = true;
}

void MainWindow::sl_drawDetercorView(CImageRGB *frame)
{
    if(_setDetector)
    {
        PTZ->Iniz(frame, CStr("marco_crowd_3"));
        //imshow("Detecort", CImageRGB2IplImage(frame));
        _setDetector = false;

    }

    if(_registra)
    {
        _registratore->AppendFrame(frame);
        _registra = false;
    }

}

void MainWindow::sl_stamp()
{

    capturePosition();
    PTZ->stampaProva(x,y,z);

    /*
    int width;
    int hiegth;

    Mat_<Point2f> Point_end;
    Mat_<Point2f> Point_end_prova;
    myHomography *prova = new myHomography();



    ifstream featureFile("Homography.yml");
    if (featureFile.is_open())
    {
       Mat Omografia;
        // carico la feature
        FileStorage fs("Homography.yml",FileStorage::READ);
        if(!fs.isOpened()){
            fs.open("Homography.yml",FileStorage::READ);
        }

            fs["Homography"] >> Omografia;
            fs.release();

        CImageRGB *tmp1 = new CImageRGB("imagesPT/PT00000.jpg");
        CImageRGB *tmp2 = new CImageRGB("imagesPT/PT00001.jpg");

        Mat_<Vec3b> Detector(CImageRGB2IplImage(tmp1));
        Mat_<Vec3b> PTZ(CImageRGB2IplImage(tmp2));
        //Mat_<Vec3b> PTZ2(CImageRGB2IplImage(tmp2));

        width = PTZ.cols;
        hiegth = PTZ.rows;


        prova->InizHomoXY("HomoProva.yml",tmp1, tmp2);

        Mat_<float> HomoGraphyProva = prova->getHXY();
        Mat_<Point2f> Point_start(1,2);
        Point_start.at<Point2f>(0) = (Point2f(x,y));


        //transform(Point_start,Point_end,Omografia);

        //circle(Detector,Point_start.at<Point2f>(0),10,Scalar(0,200,0),1,100);
        //circle(PTZ,Point_end.at<Point2f>(0),10,Scalar(0,200,0),1,100);

        QPoint Inizio(300,200);
        QPoint Arrivo;

        //prova->getTrasformPoint(Inizio,Arrivo);

        //circle(Detector,Point(Inizio.x(),Inizio.y()),20,Scalar(0,0,200),1,100);
        //circle(PTZ,Point(Arrivo.x(),Arrivo.y()),20,Scalar(0,0,200),1,100);

        Mat_<float> scale = Mat_<float>::zeros(3,3);

        scale.at<float>(0,0) = 0.25;
        scale.at<float>(2,2) = 0.25;
        scale.at<float>(3,3) = 1.0;

        transform(Point_start,Point_end_prova,HomoGraphyProva);

        circle(Detector,Point_start.at<Point2f>(0),30,Scalar(200,0,0),1,100);
        circle(PTZ,Point_end_prova.at<Point2f>(0),30,Scalar(200,0,0),1,100);

        imshow("Detector",Detector);
        imshow("PTZ",PTZ);
        //imshow("PTZ2",PTZ2);
        featureFile.close();
    }

    featureFile.open("HomographyPT.yml");
    if (featureFile.is_open())
    {
        Mat OmografiaPT;
        // carico la feature
        FileStorage fs("HomographyPT.yml",FileStorage::READ);
        if(!fs.isOpened()){
            fs.open("HomographyPT.yml",FileStorage::READ);
        }
            fs["Homography"] >> OmografiaPT;
            fs.release();


        Mat_<Point2f> PointPT;
        //transform(Point_end,PointPT,OmografiaPT);

        //Point2f tmp = PointPT.at<Point2f>(0);
        //cout << tmp.x << " " << tmp.y << endl;

        vector<PTCorrespondanceIMG> temporaneo;

        CImageRGB *tmp1 = new CImageRGB("imagesPT/PT00002.jpg");
        CImageRGB *tmp2 = new CImageRGB("imagesPT/PT00003.jpg");
        CImageRGB *tmp3 = new CImageRGB("imagesPT/PT00004.jpg");
        CImageRGB *tmp4 = new CImageRGB("imagesPT/PT00005.jpg");

        PTCorrespondanceIMG tempo1(0,10,tmp1);
        PTCorrespondanceIMG tempo2(0,-10,tmp2);
        PTCorrespondanceIMG tempo3(-10,0,tmp3);
        PTCorrespondanceIMG tempo4(10,0,tmp4);
        temporaneo.push_back(tempo1);
        temporaneo.push_back(tempo2);
        temporaneo.push_back(tempo3);
        temporaneo.push_back(tempo4);

        prova->InizHomoPT("HomoProvaPT.yml",temporaneo.begin(),temporaneo.end());



        Mat_<float> HomoPT = prova->getHPT();

        //Attenzione test per valutare il risultato

        for(Mat_<float>::iterator it=HomoPT.begin();it!=HomoPT.end();++it){
            cout<<*it<<" ";
        }
        cout<<endl;


        HomoPT.empty();
        FileStorage fs1("HomoProvaPT.yml",FileStorage::READ);
        if(!fs1.isOpened()){
            fs1.open("HomoProvaPT.yml",FileStorage::READ);
            fs1["Homograpy"] >> HomoPT;
            fs1.release();
        }

        cout<<"homopt from file"<<endl;
        for(Mat_<float>::iterator it=HomoPT.begin();it!=HomoPT.end();++it){
            cout<<*it<<" ";
        }
        cout<<endl;



        Point2f tmp11 = Point_end_prova.at<Point2f>(0);
        cout <<"Prima: " << Point_end_prova.at<Point2f>(0).x << " " << Point_end_prova.at<Point2f>(0).y << endl;
        //Point_end_prova.at<Point2f>(0)*=0.25;

        cout <<"Dopo: " << Point_end_prova.at<Point2f>(0).x << " " << Point_end_prova.at<Point2f>(0).y << endl;

        transform(Point_end_prova,PointPT,HomoPT);

        Point2f tmp22 = PointPT.at<Point2f>(0);

        double p = (tmp11.x >  width/2 ? -(tmp22.x/360) : (tmp22.x/360));
        double t = (tmp11.y >  hiegth/2 ? -(tmp22.y/180) : (tmp22.y/180));

        cout << p << " " << t << endl;
    }*/
}


MainWindow::~MainWindow()
{
    delete ui;
}
